﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace TA.Turbo
{
	/// <summary>
	/// Provides static methods for manipulating HTML markup.
	/// </summary>
	public static class HtmlTools
	{
		static Regex rgxGetScript = new Regex(@"<script\s*type=""text/javascript""\s*>(?<Script>(.|\s)+?)</script>");
		/// <summary>
		/// Returns the markup portion (excluding all scripts) of the specified HTML.
		/// </summary>
		/// <param name="html"></param>
		/// <returns></returns>
		public static string GetMarkup(string html)
		{
			return rgxGetScript.Replace(html, "").Trim();
		}
		/// <summary>
		/// Returns all of the scripts (excluding all other markup) of the specified HTML.
		/// </summary>
		/// <param name="html"></param>
		/// <returns></returns>
		public static string GetScript(string html)
		{
			MatchCollection mclScripts = rgxGetScript.Matches(html);
			StringBuilder sbScript = new StringBuilder();
			foreach (Match m in mclScripts)
				sbScript.AppendLine(m.Groups["Script"].Value);
			return sbScript.ToString();
		}
	}
}
